package com.example.demo

import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.demo.widget.chart.ChartData
import com.example.demo.widget.chart.ChartItem
import com.example.demo.widget.chart.broken.ChartBrokenLineData
import com.example.demo.widget.chart.broken.ChartBrokenLinePoint
import com.example.demo.widget.chart.histogram.HistogramData
import com.example.demo.widget.chart.histogram.HistogramItem
import com.example.demo.widget.chart.histogram.HistogramPart
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import kotlin.Comparator
import kotlin.collections.ArrayList

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val itemList = ArrayList<ChartItem>()
        itemList.add(
            ChartItem(
                "张三","10%",
                Color.parseColor("#FFF5382B"),
                12.0f
            )
        )
        itemList.add(
            ChartItem(
                "里斯123","10%",
                Color.parseColor("#FFF36699"),
                24.7f
            )
        )
        itemList.add(
            ChartItem(
                "里斯123","10%",
                Color.parseColor("#FF0F6699"),
                24.7f,true
            )
        )
        itemList.add(
            ChartItem(
                "Tom 丁","10%",
                Color.parseColor("#99000000"),
                43.3f
            )
        )
        itemList.add(
            ChartItem(
                "雷布斯","10%",
                Color.parseColor("#FFD8D8D8"),
                20.0f
            )
        )

        val data = ChartData("总数", "100.0", itemList)
        semicircleView?.setData(data)
        chartFlagsView?.setData(itemList)

        initPieView()


        initBrokenLine()

        intHistogram()
    }

    private fun initPieView() {
        val itemList = ArrayList<ChartItem>()
//        itemList.add(
//            ChartItem("张三","11111110%",
//                Color.parseColor("#FFF5382B"), 1f)
//        )
//        for(index in 0 .. 3) {
//            itemList.add(
//                ChartItem(
//                    "张三$index", "1110%",
//                    Color.parseColor("#FF666666"), 4f
//                )
//            )
//        }
//        itemList.add(
//            ChartItem("张三222","11111110%",
//                Color.parseColor("#FF666666"), 4f)
//        )
//        itemList.add(
//            ChartItem("里斯1","10%",
//                Color.parseColor("#FFF36699"),
//                43f))
        for(index in 0..3) {
            itemList.add(
                ChartItem(
                    "里斯2--$index", "10%",
                    Color.parseColor("#FFF366${0 + 2 * index}9"),
                    43f
                )
            )
        }
        for(index in 0..5) {
            itemList.add(
                ChartItem(
                    "Tom 丁", "10%",
                    Color.parseColor("#990000${0 + index}0"),
                    2.3f
                )
            )
        }
        itemList.add(
            ChartItem(
                "雷布斯","10%",
                Color.parseColor("#FFD8D8D8"),
                40.0f
            )
        )
        Collections.sort(itemList,object: Comparator<ChartItem>{
            override fun compare(o1: ChartItem?, o2: ChartItem?): Int {
                val result = (o1?.value ?: 0.0f) - (o2?.value ?: 0.0f)
                if(result == 0.0f){
                    return 0
                }else if(result > 0){
                    return 1
                }
                return -1
            }

        })
        val data2 = ChartData("总数", "100.0", itemList)
        chartPieView?.setData(data2)
    }

    private fun intHistogram() {
        val listBeijing = ArrayList<HistogramPart>()
        listBeijing.add(HistogramPart("有效里程",122000,Color.parseColor("#FF40B492")))
        listBeijing.add(HistogramPart("非有效里程",133000,Color.parseColor("#FF60A6DF")))
        listBeijing.add(HistogramPart("其他里程",111000,Color.parseColor("#FFEABE6E")))
        val beijing = HistogramItem("北京",listBeijing,366000)

        val listXiAn = ArrayList<HistogramPart>()
        listXiAn.add(HistogramPart("有效里程",200000,Color.parseColor("#FF40B492")))
        listXiAn.add(HistogramPart("其他里程",137000,Color.parseColor("#FFEABE6E")))
        val xian = HistogramItem("西安",listXiAn,337000)


        val listXuZhou = ArrayList<HistogramPart>()
        listXuZhou.add(HistogramPart("有效里程",100000,Color.parseColor("#FF40B492")))
        listXuZhou.add(HistogramPart("非有效里程",37000,Color.parseColor("#FF60A6DF")))
        listXuZhou.add(HistogramPart("里程2",27000,Color.parseColor("#FFEABE6E")))
        listXuZhou.add(HistogramPart("其他里程",120000,Color.parseColor("#FF336699")))
        val xuzhou = HistogramItem("徐州",listXuZhou,284000)


        val listNanJing = ArrayList<HistogramPart>()
        listNanJing.add(HistogramPart("有效里程",90000,Color.parseColor("#FFAF6699")))
        listNanJing.add(HistogramPart("里程1",40000,Color.parseColor("#FFBBBBBB")))
        listNanJing.add(HistogramPart("里程2",20000,Color.parseColor("#FFBBAACC")))
        listNanJing.add(HistogramPart("其他里程",30000,Color.parseColor("#FF336699")))
        val nanjing = HistogramItem("南京",listNanJing,180000)


        val listDaLi = ArrayList<HistogramPart>()
        listDaLi.add(HistogramPart("有效里程",30000,Color.parseColor("#FFAF6699")))
        listDaLi.add(HistogramPart("里程1",20000,Color.parseColor("#FFBBBBBB")))
        listDaLi.add(HistogramPart("里程2",20000,Color.parseColor("#FFBBAACC")))
        listDaLi.add(HistogramPart("其他里程",10000,Color.parseColor("#FF336699")))
        val dali = HistogramItem("大理",listDaLi,80000)

        val list = ArrayList<HistogramItem>()
        list.add(beijing)
        list.add(xian)
        list.add(xuzhou)
        list.add(nanjing)
        list.add(dali)
        list.reverse()
        val data = HistogramData(list,5)

        horizontalHistogramView?.setData(data)

    }

    private fun initBrokenLine() {
        val list = ArrayList<ChartBrokenLinePoint>()
        list.add(ChartBrokenLinePoint("北京",115))
        list.add(ChartBrokenLinePoint("上海",90))
        list.add(ChartBrokenLinePoint("呼伦贝尔大草原",87))
        list.add(ChartBrokenLinePoint("呼和浩特",80))
        list.add(ChartBrokenLinePoint("乌鲁木齐",62))
        list.add(ChartBrokenLinePoint("西安",59))
        list.add(ChartBrokenLinePoint("Bilibili测试数据哈哈哈哈",42))
        list.add(ChartBrokenLinePoint("南京",41))
        list.add(ChartBrokenLinePoint("香港特别行政区",32))
        list.add(ChartBrokenLinePoint("澳门特别行政区",11))
        val data = ChartBrokenLineData(list,5)

        chartBrokenLineView?.setData(data)
    }
}
